<template>
  <div id="details">
    <!-- 头部 -->
    <div class="header">
      <div class="title">
        <p>{{product.product_name}}</p>
        <div class="list">
          <ul>
            <li>
              <router-link to>概述</router-link>
            </li>
            <li>
              <router-link to>参数</router-link>
            </li>
            <li>
              <router-link to>用户评价</router-link>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <!-- 头部 -->

    <!-- 主要内容 -->
    <div class="main">
      <!-- 左侧轮播图 -->
      <div class="left">
        <el-carousel height="560px" v-if="productPicture.length>1">
          <el-carousel-item v-for="item in productPicture" :key="item.id">
            <img style="height:560px;" :src="$target + item.product_picture" :alt="item.intro" />
          </el-carousel-item>
        </el-carousel>
        <div v-if="productPicture.length==1">
          <img
            style="height:560px;"
            :src="$target + productPicture[0].product_picture"
            :alt="productPicture[0].intro"
          />
        </div>
      </div>
      <!-- 左侧轮播图 -->

      <!-- 右侧内容区 -->
      <div class="right">
        <h1 class="name">{{product.product_name}}</h1>
        <p class="intro">{{product.product_intro}}</p>
        <p class="store">小米自营</p>
        <div class="price">
          <span>{{product.product_selling_price}}元</span>
          <span
            class="del"
            v-show="product.product_price != product.product_selling_price"
          >{{product.product_price}}元</span>
        </div>
        <div class="pricelist">
          <!-- 中间 -->
          <span>{{product.product_name}}</span>
          <span class="price_main">
            <span>{{product.product_selling_price}}元</span>
            <span
              class="main_del"
              v-show="product.product_price != product.product_selling_price"
            >{{product.product_price}}元</span>
          </span>
          <p class="price_sum">总计 : {{product.product_selling_price}}元</p>
        </div>
        <!-- 内容区底部按钮 -->
        <div class="button">
          <el-button class="addCart" @click="addCart">加入购物车</el-button>
          <el-button class="like" @click="love">喜欢</el-button>
        </div>
        <!-- 内容区底部按钮 -->
        <div class="policy">
          <ul>
            <li>
              <i class="el-icon-circle-check"></i> 小米自营
            </li>
            <li>
              <i class="el-icon-circle-check"></i> 小米发货
            </li>
            <li>
              <i class="el-icon-circle-check"></i> 7天无理由退货
            </li>
            <li>
              <i class="el-icon-circle-check"></i> 7天价格保护
            </li>
          </ul>
        </div>
      </div>
      <!-- 右侧内容区 -->
    </div>
    <!-- 主要内容 -->
  </div>
</template>

<script>
import detailAPI from "../request/api/detailAPI";
import shoppingCartAPI from "../request/api/shoppingCartAPI";
import { mapActions } from "vuex";
export default {
  name: "VueStoreDetail",

  data() {
    return {
      productPicture: [], //轮播图
      product: {}, //商品详情
    };
  },
  // 获取商品id
  activated() {
    this.getDetailsPicture(this.$route.query);
    this.getDetails(this.$route.query);
  },
  created() {},
  mounted() {},

  methods: {
    ...mapActions(["unshiftShoppingCart", "addShoppingCartNum"]),
    // 获取轮播图
    async getDetailsPicture(ID) {
      let { data: res } = await detailAPI.getDetailsPicture(ID);
      this.productPicture = res.ProductPicture;
    },
    // 获取商品详情
    async getDetails(ID) {
      let { data: res } = await detailAPI.getDetails(ID);
      this.product = res.Product[0];
    },

    //点击喜欢
    async love() {
      //如果vuex有值说明已经登录,如果没有登录弹出登录组件
      if (!this.$store.getters.userName) {
        this.$store.commit("setShowLogin", true);
        return;
      }
      //收藏请求
      let { data: res } = await detailAPI.getaddCollect({
        user_id: this.$store.getters.getUser.user_id,
        product_id: this.product.product_id,
      });
      if (res.code == "001") {
        this.$notify({
          title: "成功",
          message: res.msg,
          type: "success",
        });
      } else {
        this.$notify.error({
          title: "错误",
          message: res.msg,
        });
      }
    },
    //加入购物车
    async addCart() {
      //如果vuex有值说明已经登录,如果没有登录弹出登录组件
      if (!this.$store.getters.userName) {
        this.$store.commit("setShowLogin", true);
        return;
      }
      // 将商品添加到购物车请求，
      let obj = {
        user_id: this.$store.getters.userInfo.user_id,
        product_id: this.product.product_id,
      };
      let { data: res } = await shoppingCartAPI.addShoppingCart(obj);
      // 判断返回码的状态，弹出对应的提示框
      if (res.code === "001") {
        // 新加入购物车成功
        this.$notify({
          title: "成功",
          message: res.msg,
          type: "success",
        });
      } else if (res.code === "002") {
        // 该商品已经在购物车，数量+1
        this.$notify.error({
          title: "错误",
          message: res.msg,
        });
      } else if (res.code === "003") {
        // 商品数量达到限购数量
        this.dis = true;
        this.$notify.error({
          title: "错误",
          message: res.msg,
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
// 头部
#details .header {
  height: 64px;
  margin-top: -20px;
  z-index: 4;
  background: #fff;
  border-bottom: 1px solid #e0e0e0;
  -webkit-box-shadow: 0px 5px 5px rgba(0, 0, 0, 0.07);
  box-shadow: 0px 5px 5px rgba(0, 0, 0, 0.07);
}
#details .header .title {
  width: 1225px;
  height: 64px;
  line-height: 64px;
  font-size: 18px;
  font-weight: 400;
  color: #212121;
  margin: 0 auto;
}
#details .header .title p {
  float: left;
}
#details .header .title .list {
  height: 64px;
  float: right;
}
#details .header .title .list li {
  float: left;
  margin-left: 20px;
}
#details .header .title .list li a {
  font-size: 14px;
  color: #616161;
}
#details .header .title .list li a:hover {
  font-size: 14px;
  color: #ff6700;
}
// 主要内容
#details .main {
  width: 1225px;
  height: 560px;
  padding-top: 30px;
  margin: 0 auto;
}
#details .main .left {
  float: left;
  width: 560px;
  height: 560px;
}
#details .el-carousel .el-carousel__indicator .el-carousel__button {
  background-color: rgba(163, 163, 163, 0.8);
}
#details .main .right {
  float: left;
  margin-left: 25px;
  width: 640px;
}
#details .main .right .name {
  height: 30px;
  line-height: 30px;
  font-size: 24px;
  font-weight: normal;
  color: #212121;
}
#details .main .right .intro {
  color: #b0b0b0;
  padding-top: 10px;
}
#details .main .right .store {
  color: #ff6700;
  padding-top: 10px;
}
#details .main .right .price {
  display: block;
  font-size: 18px;
  color: #ff6700;
  border-bottom: 1px solid #e0e0e0;
  padding: 25px 0 25px;
}
#details .main .right .price .del {
  font-size: 14px;
  margin-left: 10px;
  color: #b0b0b0;
  text-decoration: line-through;
}
#details .main .right .pricelist {
  background: #f9f9fa;
  padding: 30px 60px;
  margin: 50px 0 50px;
}
#details .main .right .pricelist span {
  line-height: 30px;
  color: #616161;
}
#details .main .right .pricelist .price_main {
  float: right;
}
#details .main .right .pricelist .price_main .main_del {
  margin-left: 10px;
  text-decoration: line-through;
}
#details .main .right .pricelist .price_sum {
  color: #ff6700;
  font-size: 24px;
  padding-top: 20px;
}
#details .main .right .button {
  height: 55px;
  margin: 10px 0 20px 0;
}
#details .main .right .button .el-button {
  float: left;
  height: 55px;
  font-size: 16px;
  color: #fff;
  border: none;
  text-align: center;
}
#details .main .right .button .addCart {
  width: 340px;
  background-color: #ff6700;
}
#details .main .right .button .addCart:hover {
  background-color: #f25807;
}

#details .main .right .button .like {
  width: 260px;
  margin-left: 40px;
  background-color: #b0b0b0;
}
#details .main .right .button .like:hover {
  background-color: #757575;
}
#details .main .right .policy li {
  float: left;
  margin-right: 20px;
  color: #b0b0b0;
}
</style>